﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Template.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the Template class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.Data
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;
    using Lidocaine.BusinessObjects;

    /// <summary>
    /// Defines the Template class.
    /// </summary>
    public static class Template
    {
        /// <summary>
        /// Initializes a new instance of the Template class.
        /// </summary>
        /// <param name="templateId">The identity of the database table's row referencing this template.</param>
        /// <returns>An instantiated Template.</returns>
        public static BusinessObjects.Template Select(int templateId)
        {
            BusinessObjects.Template template = null;
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetTemplateByID", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@ID", templateId);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();
                            template = new BusinessObjects.Template();
                            template.Id = templateId;
                            template.Area = (TemplateArea)rdr["TemplateType"];
                            template.Name = (string)rdr["TemplateName"];
                            template.TemplateText = rdr["TemplateText"].ToString();
                        }
                    }
                }
            }

            return template;
        }

        /// <summary>
        /// Gets a dictionary of templates for the current application (determined by the connection's sql user account.)
        /// </summary>
        /// <param name="templateType">The type of templates to return (Header or Footer).</param>
        /// <returns>A dictionary object containing row identity keys and the referenced template's name.</returns>
        public static List<BusinessObjects.Template> SelectAllWithoutText(TemplateArea templateType)
        {
            BusinessObjects.Template template = null;
            List<BusinessObjects.Template> templates = new List<BusinessObjects.Template>();
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetTemplates", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@TemplateType", templateType);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                template = new BusinessObjects.Template();
                                template.Id = Convert.ToInt32(rdr["TemplateID"], CultureInfo.InvariantCulture);
                                template.Name = (string)rdr["TemplateName"];
                                template.Area = templateType;
                                templates.Add(template);
                            }
                        }
                    }
                }
            }

            return templates;
        }

        /// <summary>
        /// Inserts the Template object into the database.
        /// </summary>
        /// <param name="template">The Template to insert.</param>
        /// <returns>The inserted Template with a newly populated Id field.</returns>
        public static BusinessObjects.Template Insert(BusinessObjects.Template template)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.InsertTemplate", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@TemplateType", template.Area);
                    cmd.Parameters.AddWithValue("@TemplateName", template.Name);
                    cmd.Parameters.AddWithValue("@TemplateText", template.TemplateText ?? string.Empty);
                    template.Id = Convert.ToInt32(cmd.ExecuteScalar(), CultureInfo.InvariantCulture);
                }
            }

            return template;
        }

        /// <summary>
        /// Updates the Template's database fields.
        /// </summary>
        /// <param name="template">The Template to update.</param>
        public static void Update(BusinessObjects.Template template)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.UpdateTemplate", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@TemplateID", template.Id);
                    cmd.Parameters.AddWithValue("@TemplateName", template.Name);
                    cmd.Parameters.AddWithValue("@TemplateText", template.TemplateText);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// Deletes the Template's database entry.
        /// </summary>
        /// <param name="template">The Template to delete.</param>
        public static void Delete(BusinessObjects.Template template)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                if (template.Area == TemplateArea.Header)
                {
                    using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteHeader", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@ID", template.Id);
                        cmd.ExecuteNonQuery();
                    }
                }
                else if (template.Area == TemplateArea.Footer)
                {
                    using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteFooter", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@ID", template.Id);
                        cmd.ExecuteNonQuery();
                    }
                }
                else if (template.Area == TemplateArea.LeftNav)
                {
                    using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteLeftNav", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@ID", template.Id);
                        cmd.ExecuteNonQuery();
                    }
                }
                else if (template.Area == TemplateArea.TopNav)
                {
                    using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteTopNav", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@ID", template.Id);
                        cmd.ExecuteNonQuery();
                    }
                }
                else if (template.Area == TemplateArea.RightCol)
                {
                    using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteRightCol", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@ID", template.Id);
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
    }
}